package com.tuanzi.loan.web.bean.settlementsystem;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.family.ProductInfo;
import com.tuanzi.loan.business.entity.settlementsystem.SettlementProduct;
import com.tuanzi.loan.business.service.settlementsystem.SettlementProductService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.settlementsystem.SettlementIncomeRequest;
import com.tuanzi.loan.vo.response.settlementsystem.SettlementIncomeResponse;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import com.tuanzi.loan.web.lazy.settlementsystem.SettlementIncomeLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.model.LazyDataModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;

/**
 * @author caiwanlin
 * @create 2018-08-16 20:36
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class SettlementProductInfoBean extends JSFInfoBean<SettlementProduct> {

    @Autowired
    private SettlementProductService settlementProductService;

    private List<ProductInfo> activityList;

    private List<Long> activityIdList;

    private SettlementIncomeRequest incomeRequest;

    private LazyDataModel<SettlementIncomeResponse> incomeResponseLazyDataModel;

    @PostConstruct
    private void init() {
        String id = getParamFromRequest("id");
        activityList = settlementProductService.getActivityList();
        incomeRequest = new SettlementIncomeRequest();
        if (StringUtils.isNotBlank(id)) {
            $ = settlementProductService.findOne(Long.parseLong(id));
            String activityIdStr = $.getActivityIdStr();
            if (StringUtils.isNotBlank(activityIdStr)) {
                String[] split = $.getActivityIdStr().split(",");
                activityIdList = Arrays.asList(split).stream().map(Long::parseLong).collect(Collectors.toList());
            } else {
                activityIdList = Lists.newArrayList();
            }
            //查询指定产品的消耗记录
            initIncomeLazyDataModel();

        } else {
            activityIdList = Lists.newArrayList();
            $ = new SettlementProduct();
            changeStatusToAdd();
        }
    }

    public void initIncomeLazyDataModel(){
        incomeRequest.setProductId($.getId());
        incomeResponseLazyDataModel = new SettlementIncomeLazyDataModel(incomeRequest);
    }

    public void onReset(){
        incomeRequest = new SettlementIncomeRequest();
        initIncomeLazyDataModel();
    }

    public String onSave() {
        if ($.getId() == null){
            SettlementProduct product = settlementProductService.findByProductName($.getProductName().trim());
            if (product != null){
                addErrorMessage("该产品已经存在");
                return null;
            }
        }

        String activityIdStr = null;
        ArrayList<Long> temp = new ArrayList<>();
        for (Object a : activityIdList) {
            temp.add(Long.parseLong((String) a));
        }
        activityIdStr = temp.stream().map(String::valueOf).collect(Collectors.joining(","));
        activityIdList = temp;
        $.setActivityIdStr(activityIdStr);
        $ = settlementProductService.saveOrUpdate($);
        changeStatusToView();
        addSuccessfullyMessage();
        return redirect("/views/settlementsystem/product-list.xhtml");
    }

    public void onChangeCopMode() {
        $.setPriceCpa(null);
        $.setPriceCps(null);
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(SETTLEMENT_INCOME_TYPE, COOPERATION_MODE,
                SETTLEMENT_CYCLE,SETTLEMENT_SPREAD_MODE,SETTLEMENT_SPREAD_CHANNEL);
    }
}
